package com.mike.employment.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.mike.employment.base.ApiResult;
import com.mike.employment.base.PageRequest;
import com.mike.employment.base.exception.BusinessExcep;
import com.mike.employment.po.Resume;
import com.mike.employment.service.AccountService;
import com.mike.employment.service.ResumeService;
import com.mike.employment.vo.ResumeVo;

@RestController
@RequestMapping("/api/resume")
public class ResumeController {
	public static final String separator = File.separator;
	
	@Value("${static.folder}")
    private String staticFolder;
	@Autowired
	private ResumeService resumeService;
	@Autowired
	private AccountService accountService;

	@PostMapping("/page")
	public ApiResult page(@RequestBody PageRequest req) {
		return ApiResult.success(resumeService.getPage(req));
	}
	
	@PutMapping()
	public ApiResult saveOrUpdate(@RequestBody Resume resume) {
		return ApiResult.success(resumeService.saveOrUpdate(resume));
	}

	@PostMapping()
	public ApiResult saveResume(@ModelAttribute ResumeVo vo) {
		MultipartFile docFile = vo.getDoc();
		String newName = "";
		String sourcePath = System.getProperty("user.dir") + separator +"src"+separator+ "main"+separator+"resources"+separator+ staticFolder;
		if(docFile != null && !docFile.isEmpty()){
			try {
				String fileName = docFile.getOriginalFilename();
				String timestamp = "" + new Date().getTime();		
				newName = timestamp + fileName;
				String storePath = sourcePath + separator + newName;
				BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(storePath)));
				out.write(docFile.getBytes());
				out.flush();
				out.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				throw new BusinessExcep("上传失败");
			} catch (IOException e) {
				e.printStackTrace();
				throw new BusinessExcep("上传失败");
			}
		}
		Resume resume = new Resume();
		if(vo.getId() != 0) {
			resume.setId(vo.getId());
		}
		resume.setName(vo.getName());
		resume.setDesp(vo.getDesp());
		if(!"".equals(newName)) {
			resume.setAttach(newName);
		}
		resume.setCreater(accountService.findById(vo.getAccountId()));
		return ApiResult.success(resumeService.saveOrUpdate(resume));
	}
	
	@DeleteMapping()
	public ApiResult delete(@RequestBody Long ...ids) {
		return ApiResult.success(resumeService.delete(ids));
	}
	
	@GetMapping("/list/{accountId}")
	public ApiResult myResumes(@PathVariable("accountId") Long accountId) {
		return ApiResult.success(resumeService.getMyResumes(accountId));
	}
}
